package pe.edu.upc.dsd.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pe.edu.upc.dsd.dominio.Hambur;
import pe.edu.upc.dsd.dominio.HamburLista;
import pe.edu.upc.dsd.service.HamburService;



@Controller
public class HamburController {

protected static Logger logger = Logger.getLogger("controller");
	
	@Resource(name="hamburService")
	private HamburService hamburService;
	
	@RequestMapping(value = "/hambur/{id}", 
	method = RequestMethod.GET,headers="Accept=image/jpeg, image/jpg, image/png, image/gif")
	public @ResponseBody byte[] getPhoto(@PathVariable("id") Long id) {
		try {
			InputStream is = this.getClass().getResourceAsStream("/bella.jpg"); 
			BufferedImage img = ImageIO.read(is);
			ByteArrayOutputStream bao = new ByteArrayOutputStream();
			ImageIO.write(img, "jpg", bao);
			
			logger.debug("Retrieving photo as byte array image");
			return bao.toByteArray();
			
		} catch (IOException e) {
			logger.error(e);
			throw new RuntimeException(e);
		}
	}
    
    @RequestMapping(value = "/hambur/{id}", method = RequestMethod.GET, headers="Accept=application/html, application/xhtml+xml")
	public String getHamburHTML(@PathVariable("id") Long id, Model model) {
    	logger.debug("Provider has received request with id: " + id);
		model.addAttribute("hambur",hamburService.get(id));
		return "index";
	}
    
	@RequestMapping(value = "/hambures", method = RequestMethod.GET, headers="Accept=application/xml, application/json")
	@ResponseBody
	public HamburLista getHambur() {
		logger.debug("Provider has received request to get all ");
		HamburLista result = new HamburLista();
		result.setData(hamburService.getAll());
		
		return result;
	}
	
    @RequestMapping(value = "/hambur/{id}", method = RequestMethod.GET, headers="Accept=application/xml, application/json")
	@ResponseBody
    public Hambur getPeHambur(@PathVariable("id") Long id) {
    	logger.debug("Provider has received request to get person with id: " + id);
		return hamburService.get(id);
    }
    
    @RequestMapping(value = "/hambur", method = RequestMethod.POST, headers="Accept=application/xml, application/json")
    @ResponseBody
    public Hambur addHambur(@RequestBody Hambur hambur) {
    	logger.debug("Provider has received request to add new person");
    	return hamburService.add(hambur);
    }
    
    
    @RequestMapping(value = "/hambur/{id}", method = RequestMethod.PUT, headers="Accept=application/xml, application/json")
	@ResponseBody
    public String updateHambur(@PathVariable("id") Long id, @RequestBody Hambur hambur) {
    	logger.debug("Provider has received request to edit person with id: " + id);
    	hambur.setid(id);
    	return hamburService.edit(hambur).toString();
    }
    
    
    @RequestMapping(value = "/hambur/{id}", method = RequestMethod.DELETE,headers="Accept=application/xml, application/json")
	@ResponseBody
    public String deleteHambur(@PathVariable("id") Long id) {
    	logger.debug("Provider has received request to delete person with id: " + id);
    	return hamburService.delete(id).toString();
    }
}
